package com.lul15834.orderfood.activity.user.dialog;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.function.impl.ToLong;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.OrderNoFinish;
import com.lul15834.orderfood.activity.manager.adapter.OrderNoDetailAdapter;
import com.lul15834.orderfood.activity.user.UserBuyActivity;
import com.lul15834.orderfood.activity.user.adapter.AddressAdapter;
import com.lul15834.orderfood.activity.user.adapter.BuyFoodDetailAdapter;
import com.lul15834.orderfood.bean.AddressBean;
import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.bean.OrderDetailBean;
import com.lul15834.orderfood.bean.UserComBean;
import com.lul15834.orderfood.dao.AddressDao;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.dao.FoodDao;
import com.lul15834.orderfood.dao.OrderDao;
import com.lul15834.orderfood.until.Tools;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class UserBottomDialog  {
    private Context context;
    UserBuyActivity man;
    private String businessId;
    public UserBottomDialog(Context context,String businessId){
        man = (UserBuyActivity) context;
        this.context=context;
        this.businessId=businessId;
        init();
    }
    private void init(){
        View bottomSheetLayout=man.getLayoutInflater().inflate(R.layout.user_buy_food_bottom_menu_dialog,null);
        BottomSheetDialog bottomSheetDialog=new BottomSheetDialog(context);
        bottomSheetDialog.setContentView(bottomSheetLayout);
        bottomSheetDialog.show();
        UserComBean user= AdminDao.getComUser(Tools.getOnAccount(bottomSheetLayout.getContext()));
        //實現加載頭像
        ImageView img=bottomSheetLayout.findViewById(R.id.buy_food_dialog_img);
        img.setImageBitmap(BitmapFactory.decodeFile(user.getsImg()));
        TextView name=bottomSheetLayout.findViewById(R.id.buy_food_dialog_name);
        name.setText(user.getsName());
        TextView userTime=bottomSheetLayout.findViewById(R.id.buy_food_dialog_time);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time=simpleDateFormat.format(date);
        userTime.setText(time);
        //加载收货信息
        TextView people=bottomSheetLayout.findViewById(R.id.buy_food_dialog_people);
        people.setText("");
        TextView address=bottomSheetLayout.findViewById(R.id.buy_food_dialog_address);
        address.setText("");
        TextView phone=bottomSheetLayout.findViewById(R.id.buy_food_dialog_phone);
        phone.setText("");
        RecyclerView addressList=bottomSheetLayout.findViewById(R.id.buy_food_dialog_list);
        //查询用户地址
        List<AddressBean> list=AddressDao.getAllAddressByUserId(Tools.getOnAccount(context));
        AddressAdapter addressAdapter=new AddressAdapter(bottomSheetLayout,list);
        addressList.setLayoutManager(new LinearLayoutManager(context));
        if (list==null||list.size()==0){
            addressList.setAdapter(null);
        }else {
            addressList.setAdapter(addressAdapter);
        }
        RecyclerView listView=bottomSheetLayout.findViewById(R.id.buy_food_dialog_food_list);
        //获取所有商品信息
        TextView buyFood=man.findViewById(R.id.user_buy_business_food);
        String foodList=buyFood.getText().toString();
        JSONArray jsonArray=JSONArray.parseArray(foodList);
        List<OrderDetailBean> list1=new ArrayList<>();//图片 价格 名称
        for (Object o:jsonArray){
            JSONObject temp=JSONObject.parseObject(o.toString());
            OrderDetailBean orderDetailBean=new OrderDetailBean();
            orderDetailBean.setFoodId(temp.getString("foodId"));
            orderDetailBean.setFoodQuantity(temp.getString("num"));
            if (temp.getString("num").equals("0")){
                continue;
            }
            FoodBean food=  FoodDao.getAllFoodByFoodId(temp.getString("foodId"));
            orderDetailBean.setFoodPrice(food.getFoodPrice());
            orderDetailBean.setFoodImg(food.getFoodImage());
            orderDetailBean.setFoodName(food.getFoodName());
            orderDetailBean.setFoodDescription(food.getFoodDescription());
            list1.add(orderDetailBean);
        }
        BuyFoodDetailAdapter buyFoodDetailAdapter=new BuyFoodDetailAdapter(context,list1);
        listView.setLayoutManager(new LinearLayoutManager(context));
        if (list1==null||list1.size()==0){
            listView.setAdapter(null);
        }else {
            listView.setAdapter(buyFoodDetailAdapter);
        }
        TextView price=man.findViewById(R.id.user_buy_business_price);
        TextView sumPrice=bottomSheetLayout.findViewById(R.id.buy_food_dialog_sumPrice);
        sumPrice.setText(price.getText().toString());
        Button cancel=bottomSheetLayout.findViewById(R.id.buy_food_dialog_cancelBtn);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.cancel();
                Toast.makeText(v.getContext(), "取消支付", Toast.LENGTH_SHORT).show();
            }
        });
        Button finish=bottomSheetLayout.findViewById(R.id.buy_food_dialog_finishBtn);
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建订单向详情表订单表插入数据
                if (people==null||people.getText().toString().equals("")){
                    Toast.makeText(v.getContext(), "请选择收获地址", Toast.LENGTH_SHORT).show();
                }else
                if (address==null||people.getText().toString().equals("")){
                    Toast.makeText(v.getContext(), "请选择收获地址", Toast.LENGTH_SHORT).show();
                }else
                if (phone==null||phone.getText().toString().equals("")){
                    Toast.makeText(v.getContext(), "请选择收获地址", Toast.LENGTH_SHORT).show();
                }else {
                    String address1=people.getText().toString()+"-"+address.getText().toString()+"-"+phone.getText().toString();
                    String orderId= UUID.randomUUID().toString().replace("-","");
                    String orderDetailId= UUID.randomUUID().toString().replace("-","");
                    int a=OrderDao.insertOrderSta(orderId,time,businessId,user.getsId(),orderDetailId,"1",address1);
                    if (a==1){
                        for (OrderDetailBean orderDetailBean : list1) {//所有的订单详情信息
                            orderDetailBean.setDetailId(orderDetailId);
                            OrderDao.saveOrderDetail(orderDetailBean);
                            Toast.makeText(v.getContext(), "完成支付", Toast.LENGTH_SHORT).show();}
                            bottomSheetDialog.cancel();
                    }else {
                        Toast.makeText(v.getContext(), "购买失败", Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });
    }
}
